<?php
// +----------------------------------------------------------------------
// | YFCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2015-2016 http://www.rainfer.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: rainfer <81818832@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use think\Db;

class Upload extends Base
{
    public function index()
    {   
        $data = input();
        $type = input('file_type', 1);
        $this->assign('formInfo', json_encode($data, JSON_UNESCAPED_UNICODE));
        //附件
        if ($type == 2) {
            return $this->fetch('file_index');
        } elseif ($type == 3){
            return $this->fetch('moulds');
        }
        return $this->fetch();
    }
    

    public function upload(){
        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('file');
        $formInfo = json_decode(input('formInfo'), true);
        // var_dump($file);exit();
        // 移动到框架应用根目录/public/uploads/ 目录下
        if($file && $formInfo){
            $info = $file->validate(['size'=>10485760,'ext'=>'jpg,png,gif,xls,xlsx,doc,docx,pdf,zip,rar,mp4'])->move(ROOT_PATH . 'public' . DS . 'uploads');
            if($info){
                $url = $info->getSaveName();
                //保存至项目或者事项
                $data = [];
                $data['create_uid'] = session('userinfo.id')?session('userinfo.id'):0;
                $data['create_time'] = time();
                $data['doc_url'] = $url;

                if ($formInfo['type'] == 'proj')
                {   
                    $data['status'] = session('userinfo.org_id')==1?1:0;
                    $data['file_type'] = isset($formInfo['file_type'])?$formInfo['file_type']:1;
                    $data['doc_id'] = $formInfo['doc_id'];
                    $data['proj_id'] = $formInfo['proj_id'];
                    $data['build_id'] = $formInfo['build_id'];
                    $data['sort'] = db('proj_doc')->where(['proj_id'=>$formInfo['proj_id'],
                        'build_id'=>$formInfo['build_id'],'doc_id'=>$formInfo['doc_id']])->count();
                    //权限判断
                    $auth_check = model("AuthDoc")->check_auth($formInfo['doc_id'], 'write');
                    if (!$auth_check){
                        return json(['code'=>1,'msg'=>'您没有资料上传权限,上传失败!']);
                    }
                    
                    $res = db('proj_doc')->insert($data);
                    
                } elseif ($formInfo['type'] == 'schedule'){
                    $picture = $url;
                    
                    if($picture){
                        return json(['code'=>0,'msg'=>'上传成功','picture'=>$picture]);
                    }else{
                        return json(['code'=>1,'msg'=>'上传失败']);
                    }
                    
                }  elseif ($formInfo['type'] == 'upload_all'){
                    $data['order_id'] = $formInfo['order_id'];
                    $res = db('proj_doc_temp')->insert($data);
                }else {
                    $data['case_doc_id'] = $formInfo['case_doc_id'];
                    $data['source'] = 2;
                    $data['proj_id'] = $formInfo['proj_id'];
                    $data['build_id'] = $formInfo['build']=='build_id'?0:$formInfo['build'];
                    $data['file_type'] = isset($formInfo['file_type'])?$formInfo['file_type']:1;
                    $res = db('case_doc_list')->insert($data);
                }
                if ($res) {
                    return json(['code'=>0,'msg'=>'上传成功!']);
                } else {
                    return json(['code'=>1,'msg'=>'上传失败!']);
                }
            }else{
                // 上传失败获取错误信息
                return  json(['code'=>1,'msg'=>$file->getError()]);
            }
        } else {
            return  json(['code'=>1,'msg'=>'缺少数据,上传失败!']);
        }
    }
    
    
    public function del()
    {
        $id = input('id/d',0);
        $type = input('type','');
        if (empty($id) || empty($type)) {
            $this->error('资料ID不存在！');
        }
        $doc_info = [];
        if ($type == 'proj') {
            $doc_info = db('proj_doc')->where('id', $id)->find();
            $auth_check = model("AuthDoc")->check_auth($doc_info['doc_id'],'del');
            
            if (!$auth_check && $doc_info['status']==1){
                $this->error('您没有该资料删除权限！');
            }
            
            $delete = db('proj_doc')->where('id', $id)->delete();
        } else {
            $doc_info = db('case_doc_list')->where('id', $id)->find();
            $delete = db('case_doc_list')->where('id', $id)->delete();
        }
        if ($delete) {
            $this->success('删除成功！','',['id'=>$id,'status'=>$doc_info['status']]);
        } else {
            $this->error('删除失败！');
        }
    }
    
    
    public function tempCreate()
    {
        $input = input();
        $temp_name = $input['temp_name'];
        
        if ($temp_name) {
            $this->assign('input',$input);
            return $this->fetch('temp/'.$temp_name);
        }
    }
    
    
    public function tempHandle()
    {
        $formInfo = input('data/a');
        $html = input('html');
        if ($html && $formInfo) {
            $html = '<html xmlns="http://www.w3.org/1999/xhtml">'.$html.'</html>';
            $name = uniqid();
            $filename = '/data/wwwroot/doc/public/temp_html/'.$name;
            $pdfname = '/data/wwwroot/doc/public/uploads/temp_html/'.$name;
            $url = 'http://doc.pkjyzx.com/public/temp_html/'.$name.'.html';
            file_put_contents("{$filename}.html", $html);
            ob_end_clean();
            shell_exec("wkhtmltopdf -q {$url} {$pdfname}.pdf");
            $pdfname = $pdfname.'.pdf';
            if (file_exists($pdfname)){
                $data = [];
                $data['create_uid'] = session('userinfo.id')?session('userinfo.id'):0;
                $data['create_time'] = time();
                $data['doc_url'] = 'temp_html/'.$name.'.pdf';
                $data['case_doc_id'] = $formInfo['case_doc_id'];
                $data['source'] = 2;
                $data['proj_id'] = $formInfo['proj_id'];
                $data['build_id'] = $formInfo['build']=='build_id'?0:$formInfo['build'];
                $data['file_type'] = 2;
                $res = db('case_doc_list')->insert($data);
                if ($res) {
                   return ['code'=>1,'msg'=>'生成成功！']; 
                }
            }
        }
        return ['code'=>0,'msg'=>'生成失败！'];
    }        
}
